package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.GetUpBuyHouseListAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.bean.GetUpBuyHouseBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * 放弃购房
 */
public class GetUpBuyHouseActivity extends BasePersonActivity {

    List<GetUpBuyHouseBean> mGetUpBuyHouseBeans = new ArrayList<>();
    @BindView(R.id.recycle_activity_get_up_buy_house)
    RecyclerView mRecycleActivityGetUpBuyHouse;
    private LoadingPagerHead mLoadingPagerHead;
    private GetUpBuyHouseListAdapter mGetUpBuyHouseListAdapter;

    @Override
    protected View getContentView(Context context) {
     //   setContentView(R.layout.activity_get_up_buy_house);
        mLoadingPagerHead = new LoadingPagerHead(this) {
            @Override
            protected void close() {

            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(GetUpBuyHouseActivity.this, R.layout.activity_get_up_buy_house);
            }

            @Override
            protected void reLoading() {
                retry();

            }
        };


        return mLoadingPagerHead;
    }

    /**
     * 重新加载
     */
    private void retry() {
        Observable<StatusCode<List<GetUpBuyHouseBean>>> upBuyHouseBeanObservable = ApiUtils.getApi().getUpBuyHouse(BaseApplication.getToken(), BaseApplication.getUserId(), BaseApplication.sUserInfoBean.getUsername());
        HttpUtil.getInstance().toSubscribe(upBuyHouseBeanObservable, new ProgressSubscriber<List<GetUpBuyHouseBean>>(this) {


            @Override
            protected void _onNext(StatusCode<List<GetUpBuyHouseBean>> listStatusCode) {
                List<GetUpBuyHouseBean> data = listStatusCode.getData();

                if (listStatusCode.isFlag() && data != null && data.size() > 0) {
                    mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                    mGetUpBuyHouseBeans.clear();
                    mGetUpBuyHouseBeans.addAll(data);
                    mGetUpBuyHouseListAdapter.notifyDataSetChanged();

                } else {
                    mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                }

            }

            @Override
            protected void _onError(String message) {
                ToastUtils.makeText(message);
                mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);

            }
        }, "", lifecycleSubject, false, true);


    }

    @Override
    protected void initData() {
        super.initData();
        mTvTitle.setVisibility(View.VISIBLE);
        mTvTitle.setText("放弃签约");
        mGetUpBuyHouseListAdapter = new GetUpBuyHouseListAdapter(this, mGetUpBuyHouseBeans);
        mRecycleActivityGetUpBuyHouse.setAdapter(mGetUpBuyHouseListAdapter);

        //设置布局管理器
        mRecycleActivityGetUpBuyHouse.setLayoutManager(new LinearLayoutManager(this));
        //设置adapter
        mRecycleActivityGetUpBuyHouse.setAdapter(mGetUpBuyHouseListAdapter);
        //设置Item增加、移除动画
        mRecycleActivityGetUpBuyHouse.setItemAnimator(new DefaultItemAnimator());

        retry();
    }
    public static void startActivity(Context context){
        Intent intent=new Intent(context,GetUpBuyHouseActivity.class);
        context.startActivity(intent);
    }
}
